package cn.com.yang.ocr;

import org.junit.Test;
import org.opencv.core.Core;
import org.opencv.core.Mat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.opencv.core.Core.merge;
import static org.opencv.core.Core.split;
import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgproc.Imgproc.equalizeHist;

public class TestOcr {


    /**
     * 1.Not a JPEG file: starts with 0x89 0x50(改了图片后缀导致)
     * 2.javax.imageio.IIOException: I/O error reading PNG header!
     * Caused by: javax.imageio.IIOException: Bad PNG signature!
     *
     * @param args
     */
    public static void main(String[] args) {

        //输入图片地址
        String path = "F:\\使用文档\\图片文件\\知识整理.png";
//        PictureManage pictureManage = new PictureManage(path); //对图片进行处理
//        pictureManage.imshow("jpg");

        try {
            String valCode = new OCR().recognizeText(new File(path), "png");//jpg是图片格式
            System.out.println("图片中文字为："+"\n"+valCode);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * TestEngine with ID 'junit-jupiter' failed to discover tests
     * @throws Exception
     */

    public void testOpenCV() throws Exception {
//        URL url = ClassLoader.getSystemResource("lib/opencv_java420.dll");
//        System.load(url.getPath());
        System.load("D:\\迅雷下载\\opencv\\build\\java\\x64\\opencv_java453.dll");
        //打印OpenCV的版本
        System.out.println("OpenCV : " + Core.VERSION);
        //填你的图片地址
        Mat image = imread("F:\\1234.png", 1);
        if (image.empty()){
            throw new Exception("image is empty!");
        }
        imshow("Original Image", image);
        List<Mat> imageRGB = new ArrayList<>();
        split(image, imageRGB);
        for (int i = 0; i < 3; i++) {
            equalizeHist(imageRGB.get(i), imageRGB.get(i));
        }
        merge(imageRGB, image);
        imshow("Processed Image", image);
        waitKey();
    }
    
    public void test_JPG_Linux(){
        String tessPath = "/your/tesseract/path/Tesseract-OCR";
        String img = "/your/test/file/path/test.jpg";
        OcrProcess ocr = new OcrProcess(tessPath);
        File file = new File(img);
        try {
            String path = ocr.doOCR(file, "chi_sim");
            System.out.println(path);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void test_JPG_Windows(){

        String tessPath = "D:\\your\\tesseract\\path\\Tesseract-OCR";
        String img = "C:\\your\\test\\file\\path\\test.jpg";

        OcrProcess ocr = new OcrProcess(tessPath);
        //测试保存其他目录
        try {
            ocr.setSaveDir("C:\\others\\save\\path");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        File file = new File(img);
        try {
            String path = ocr.doOCR(file, "chi_sim");
            System.out.println(path);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
